<?php

namespace spider;


use think\Exception;

defined('SPIDER_HTML_PATH') or define('SPIDER_HTML_PATH', RUNTIME_PATH . 'html' . DS);

/**
 * 爬虫总类
 */
class Spider
{
    //平台列表
    public static $platformList = [
        ['name' => 'teepublic', 'className' => '\spider\Teepublic'],
    ];

    /**
     * 执行抓取任务
     * @param $params
     * @return array
     */
    public static function spider($params)
    {
        $data = ['platform' => '', 'list' => []];
        foreach (self::$platformList as $platform) {
            if (strpos($params['get_url'], $platform['name']) !== false) {
                $data = $platform['className']::spider($params);
                $data['platform'] = $platform['name'];
                return $data;
            }
        }
        return $data;
    }

    /**
     * 保存图片到本地
     * @param $params
     * @return bool
     */
    public static function saveShopImage($params)
    {
        try {
            $localPath = ROOT_PATH . "public" . $params['local_img_url'];
//            dump($params['images']);
//            dump($localPath);
            return saveShopImage($params['images'], $localPath);
        } catch (Exception $exception) {
            return false;
        }
    }


}
